import React, {Component} from 'react'

/**
 * @importComponent 导入的路由组件
 * */

export default function asyncRouter(importComponent) {
  class AsyncRouter extends Component {
    constructor(props) {
      super(props);

      this.state = {
        component: null
      };
    }

    async componentDidMount() {
      const {default: component} = await importComponent();
      this.setState({component})
    }

    render() {
      const Component = this.state.component;
      // 标签不允许小写
      return Component ? <Component {...this.props}/> : null
    }

  }

  return AsyncRouter;
}